package com.zw.xxh.udp;

import com.zw.xxh.util.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.DatagramPacket;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class UdpClientHandle extends ChannelInboundHandlerAdapter {

    private static String hexString = "8060000011c8dc8000000001000001ba451c8ee40401005f6bf8000001bb000c80ccf504e17fe0e0e8c0c020000001bc0012e1ff000000081be0000090c0000000000000000001e0e4e6888005214723b901000000016742801f965200a00b749404040500000300010000030032840000000168cb8d48000000016588804002dbe44fcbfd42a0002007e71b66a708c9eaca72950c217b3411b3672c442117f366d9cbb07000271c00087c9efe5f5489ed9b95b7dcddae03cb56b0b25f3d279c40ea43427daeaa08e9b9905f1d5d6df2789dcb195f0e3c69288c28f5b026129e3378160fbd0db4dff50f35750b7a5cf87ffdd96c86035136c82c388905e57001036642dd9ae6b51ca3588f18abfff3eea532e5d6525180a17959cc09c914cd1938abef32c5fffd3eae82980ffab5c99462b548fe96778d152659425a65ec46c3e37f5feffd13e3775b8956b71615e93cbe161de1a30099e699029ef494595ca3b32dbf90cd8ec94218047e7bc37519f5fc14bd1389d2b22c56df51533b0dc3c302bbcdf70fbc17eb88c4bbedf94f72eaf13f820074b87ffea4a4e13a42530596ab233e9aaf927d8f7853445e044b8e78b252c18c2a66636c0bc56b508600beeb01237212c0bd7dc1bb5e1f7d21ed9ceb52af71e6055ee1cc131d91bbfeb2b1102181de38424e571012fcf0e77b44ff64a19acb9eb1d41fa7abd5eec676720abd9e9bc317e87f612faf946f335279c29f6d2e64e8f2c318aa30eaea4c95f8330d4d9fbdd8fde53808ec2b615a5fa2452490d179829daede9b3800040049a78b5509b3236890e6a2cc7dfa31b1577ebe01fc3c20c747e86aaf72e32bb93d173714fb4f33ff9d5793c24e928fe047918faf89ad1f325e436b7bcae69e9017fea469c9e8d6ef7c1e21182001dc3a3bca6575d47e1686014e9ea37b3b4e36561be619be0d9e40ec93dc9ee1a969ffc6ccebe0dbbc5a3d9a4d00ee5418970d3bfa712dfd519e22a2d5ffbb07fff7d6947e7e95cf00042ee239af837ff615a2028801a0520e11eafa23ec19267bc6fc454222016b42b26664ddca7ca814c20d9ad6f7e85c3b7d45a6a97a12944d8a8cd31d005205aec37503281d704d8ca955a9a8cb987ffffee1de4f370239e0f5e626919bb642968ac000400a94d22e1d5abd30ad69e7433359c2db9239e0361b86da7ffcbdd2955dea7ec51a93560df58254f1ebffffb9cb04da2a4c51129a8a0b41a8a12d310b4cbfacbfffff7184c34c1601b602a5a2e3d7a4f5ef15206a6ad3c263fcce98d12876e8ef2be8d4bffe7beee40d0f03ebc3867a260015102852b6833ddea1ffc1f3c6d13ffff72c19c786a2cae4631701ca2a1854c4ea6f29d318c3b542dc64fa89c9541ffffab01fc1d11a8f5d55068619314ed34fd86f54997a94ae8f4213524f415ffffd6ea983de06a7062fc847841c2a39924a09d96287b5550f54db503fffff74a029ad14ecc31807529307e4d319dcc5179d92b306e62ffdbd0fea7d30d8000886517bf1cc2dac90360301ef5796a54da94183c16747ea47d96b3d22e6713141efbe23de9e5dffdbb02c74ead7add7cc7e11418da809e3690783cb498fdb13c0ddfc62813b9995f77bfe48ad70c60474b6b0ee61f775369e0eacc464d6e1be39f6ecb50a7c7e4a6bd49c2228a6aa58afe2e37ce6a85c73bf5b788ff098a837f06c2ff8d3ee8756bdafc4a5ccf7b70880795dcea70c33bccd76b607d4904e93f40637d039cc4354907eb24075f1728e9e93281843052db63f79d4fffa27d6a7554dc243754bc9c574e8ef5ddf53af0dc6dc918fafebbb99761e2882980579cce3088baf6c8dec164464fb02174f30ca3482d6d48cf7ca301a4179b0f07508601d7d4d8203ef90e8a899d70867358e9c9f0237737d8733c49f78c3cb921f969210c08c19ec25eca6e6e590d922342129456d32b03bc1ab9692cb9c37ffee03f9a96014cb475116d9d9ce9a2ed61cc24531ca68d2761eba942731f8fff70f43a120d8730d104c440d92c19e94ba51166b5291";
    private static byte[] data = ByteUtils.convertHexStringToByteArray(hexString);
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("准备发送");
        for (int i = 0; i < 20; i++) {
            ByteBuf byteBuf = ctx.channel().alloc().buffer();
            ByteBuffer byteBuffer = ByteBuffer.allocate(Integer.BYTES);
            byteBuffer.putInt(i);
            byte[] re = byteBuffer.array();
            data[2] = re[2];
            data[3] = re[3];
            ctx.writeAndFlush(new DatagramPacket(byteBuf.writeBytes(data), new InetSocketAddress("192.168.8.184", 6004)));
            Thread.sleep(2);
            if (i % 10 == 0) {
                System.out.println("已发送： " + i + " 个包");
            }
        }
        System.out.println("发送完毕");
        System.out.println("data: " + Arrays.toString(data));
    }
}
